class_name CounterBasedEffect
extends Effect

# 基于计数器的效果示例
# 演示如何使用计数器系统创建条件性效果

@export var counter_type: TurnCounter.CounterType = TurnCounter.CounterType.HEALTH_LOST
@export var condition_type: String = "at_least"  # 条件类型
@export var threshold: int = 5  # 阈值
@export var base_damage: int = 10  # 基础伤害
@export var bonus_damage_per_point: int = 2  # 每点计数器值的额外伤害

func execute(targets: Array[Node]) -> void:
	print("🎯 基于计数器的效果触发")
	
	for target in targets:
		if target is Player:
			var player = target as Player
			var counter = player.stats.get_turn_counter()
			
			if counter:
				var counter_value = counter.get_counter(counter_type)
				var condition_met = counter.check_condition(counter_type, condition_type, threshold)
				
				print("📊 计数器检查: %s = %d, 条件: %s %d, 结果: %s" % [
					TurnCounter.CounterType.keys()[counter_type],
					counter_value,
					condition_type,
					threshold,
					"满足" if condition_met else "不满足"
				])
				
				if condition_met:
					# 计算伤害
					var damage = base_damage + (counter_value * bonus_damage_per_point)
					
					print("💥 条件满足！造成伤害: %d (基础: %d + 计数器: %d × %d)" % [
						damage, base_damage, counter_value, bonus_damage_per_point
					])
					
					# 这里可以应用具体效果，比如造成伤害、获得护盾等
					_apply_conditional_effect(player, damage, counter_value)
				else:
					print("❌ 条件不满足，效果不触发")
			else:
				print("❌ 无法获取计数器")

func _apply_conditional_effect(player: Player, damage: int, counter_value: int) -> void:
	# 示例：根据计数器值应用不同效果
	match counter_type:
		TurnCounter.CounterType.HEALTH_LOST:
			# 如果是基于失血量，则给予护盾
			player.stats.gain_block(damage)
			print("🛡️ 根据失血量获得 %d 护盾" % damage)
			
		TurnCounter.CounterType.MANA_SPENT:
			# 如果是基于能量消耗，则回复血量
			player.stats.heal(damage)
			print("❤️ 根据能量消耗回复 %d 血量" % damage)
			
		TurnCounter.CounterType.MANA_OVERSPENT:
			# 如果是基于过量消耗，则获得更强的效果
			var bonus_effect = counter_value * 2  # 过量消耗有更高的回报
			player.stats.gain_block(bonus_effect)
			player.stats.heal(bonus_effect)
			print("💰 根据过量消耗获得 %d 护盾和 %d 血量（过量消耗奖励）" % [bonus_effect, bonus_effect])
			
		TurnCounter.CounterType.CARDS_PLAYED:
			# 如果是基于卡牌使用数，则获得能量
			player.stats.gain_mana(counter_value)
			print("⚡ 根据卡牌使用数获得 %d 能量" % counter_value)
			
		_:
			print("🎲 应用默认效果")

# 创建特定条件的快捷方法
static func create_health_based_effect(threshold: int, base_effect: int) -> CounterBasedEffect:
	var effect = CounterBasedEffect.new()
	effect.counter_type = TurnCounter.CounterType.HEALTH_LOST
	effect.condition_type = "at_least"
	effect.threshold = threshold
	effect.base_damage = base_effect
	return effect

static func create_mana_based_effect(threshold: int, base_effect: int) -> CounterBasedEffect:
	var effect = CounterBasedEffect.new()
	effect.counter_type = TurnCounter.CounterType.MANA_SPENT
	effect.condition_type = "at_least"
	effect.threshold = threshold
	effect.base_damage = base_effect
	return effect

static func create_overspend_based_effect(threshold: int, base_effect: int) -> CounterBasedEffect:
	var effect = CounterBasedEffect.new()
	effect.counter_type = TurnCounter.CounterType.MANA_OVERSPENT
	effect.condition_type = "at_least"
	effect.threshold = threshold
	effect.base_damage = base_effect
	return effect

static func create_card_based_effect(threshold: int, base_effect: int) -> CounterBasedEffect:
	var effect = CounterBasedEffect.new()
	effect.counter_type = TurnCounter.CounterType.CARDS_PLAYED
	effect.condition_type = "at_least"
	effect.threshold = threshold
	effect.base_damage = base_effect
	return effect 
